package com.haizhi.crm.biz.task.message.handler;

import com.haizhi.crm.biz.builder.MsgSearchQoBuilder;
import com.haizhi.crm.biz.constant.MessageConstant;
import com.haizhi.crm.biz.model.MessageClientDo;
import com.haizhi.crm.biz.task.message.model.ContextDto;
import com.haizhi.crm.biz.util.MessageUtil;
import com.haizhi.crm.search.model.SearchQo;
import com.haizhi.crm.search.model.SearchVo;
import com.haizhi.crm.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * Created by wangshan on 2018/5/29.
 */
@Component
public class CompanyHandler extends AbstractHandler<ContextDto> {

    @Autowired
    private SearchService searchService;


    @Override
    protected MsgResponse process(ContextDto data) {
        List<MessageClientDo> keys = data.getMessageClientDos();
        if (keys == null || keys.size() == 0) {
            return MsgResponse.error("key is null" );
        }
        for (MessageClientDo messageClientDo : keys) {
            SearchQo searchQo = MsgSearchQoBuilder.get(data.getDatabases(), messageClientDo.getCompanyId());
            SearchVo searchVo = searchService.search(searchQo);
            List<Map<String, String>> datas = MessageUtil.toList(searchVo.getSearchResp().getData());
            Map<String, String> company = datas.get(0);
            messageClientDo.setCompanyName(company.get(MessageConstant.NAME));
            if (company.get(MessageConstant.IS_CUSTOMER).equals(MessageConstant.TRUE))
                messageClientDo.setInner(true);
            else
                messageClientDo.setInner(false);
        }
        return MsgResponse.success();
    }
}
